package com.example.wuyeguanli.tools;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.Repair;
import com.example.wuyeguanli.entiy.Response;
import java.util.List;

public class RepairAdapter extends RecyclerView.Adapter<RepairAdapter.RepairViewHolder> {
    private final String TAG = "ApiClient";
    private List<Repair> repairs;
    private final ApiClient apiClient = new ApiClient();
    private Context context;

    public RepairAdapter(List<Repair> repairs, Context context) {
        this.repairs = repairs;
        this.context = context;
    }

    @NonNull
    @Override
    public RepairViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.repairlist_windows, parent, false);
        return new RepairViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RepairViewHolder holder, int position) {
        Repair currentItem = repairs.get(position);

        holder.repairNum.setText(currentItem.getWorkOrderId());
        holder.repairText.setText(currentItem.getMessage());
        holder.repairBoolean.setText(currentItem.isHandled() ? "已完成" : "待处理");

        // 根据图片ID加载图片
        if (currentItem.getImageId() != null && !currentItem.getImageId().isEmpty()) {
            holder.repairImage.setVisibility(View.VISIBLE);
            // 拼接完整的图片URL，注意要用你的服务器基础URL
            String imageUrl = ApiClient.BASE_URL + "/image/" + currentItem.getImageId();
            GlideApp.with(context)
                    .load(imageUrl)
                    .placeholder(R.drawable.ic_loading_placeholder) // 加载中的占位图
                    .error(R.drawable.ic_load_error_placeholder)      // 加载失败的占位图
                    .into(holder.repairImage);
        } else {
            holder.repairImage.setVisibility(View.GONE);
        }

        // 处理“报修完成”按钮的显示和点击事件
        if (currentItem.isHandled()) {
            holder.repairButton.setVisibility(View.GONE);
        } else {
            holder.repairButton.setVisibility(View.VISIBLE);
            holder.repairButton.setOnClickListener(v -> {
                holder.repairButton.setEnabled(false);
                apiClient.finishRepair(currentItem.getWorkOrderId(), new ApiClient.OnCompleteListener<Response<Object>>() {
                    @Override
                    public void onSuccess(Response<Object> data) {
                        if (data != null && "success".equals(data.getState())) {
                            currentItem.setHandled(true);
                            holder.repairBoolean.setText("已完成");
                            holder.repairButton.setVisibility(View.GONE);
                            Toast.makeText(context, "操作成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, data != null ? data.getError() : "操作失败", Toast.LENGTH_SHORT).show();
                            holder.repairButton.setEnabled(true);
                        }
                    }

                    @Override
                    public void OnError(Throwable error) {
                        Log.e(TAG, "FinishRepair OnError: ", error);
                        Toast.makeText(context, "网络错误，请稍后重试", Toast.LENGTH_SHORT).show();
                        holder.repairButton.setEnabled(true);
                    }
                });
            });
        }
    }

    @Override
    public int getItemCount() {
        return repairs.size();
    }

    public static class RepairViewHolder extends RecyclerView.ViewHolder {
        TextView repairNum, repairText, repairBoolean;
        ImageView repairImage;
        Button repairButton;

        public RepairViewHolder(@NonNull View itemView) {
            super(itemView);
            // 确保ID与repairlist_windows.xml中的ID一致
            repairNum = itemView.findViewById(R.id.repair_num);
            repairText = itemView.findViewById(R.id.repair_text);
            repairBoolean = itemView.findViewById(R.id.repair_boolean);
            repairImage = itemView.findViewById(R.id.repair_image);
            repairButton = itemView.findViewById(R.id.repair_buttom);
        }
    }
}